#   通过实例理解xpath解析，巩固xpath表达式
#   学者 小韦
#   2022年7月2日 阵雨转晴 湖南娄底娄星区

#需求：爬取58二手房中的房源信息
import requests
from lxml import etree


#1.爬取页面源码数据
#UA伪装
if __name__ == "__main__":
    header = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                     'Chrome/101.0.0.0 Safari/537.36'}
    url = 'https://www.58.com/ershoufang/'
    page_text = requests.post(url=url,headers=header).text
# print(page_text)
#2.数据解析
    tree = etree.HTML(page_text)

    tr_list = tree.xpath('//div[@id="global"]/table//tr')
    fp = open('58.txt','w',encoding='utf-8')
    for tr in tr_list:
        title = tr.xpath('./td[2]/a/text()')[0]
        print(title)
        fp.write(title+'\n')

